package co.yixiang.yshop.module.order.dal.mysql.orderfeedback;

import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.module.order.dal.dataobject.orderfeedback.OrderFeedbackDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 订单反馈 Mapper
 *
 * @author yshop
 */
@Mapper
public interface OrderFeedbackMapper extends BaseMapperX<OrderFeedbackDO> {

    /**
     * 检查用户是否是复购某商品
     * @param userId 用户ID
     * @param productId 商品ID
     * @param excludeOrderId 排除的订单ID
     * @return 购买次数
     */
    @Select("SELECT COUNT(*) FROM yshop_store_order_cart_info oi " +
            "JOIN yshop_store_order o ON oi.oid = o.id " +
            "WHERE o.uid = #{userId} AND oi.product_id = #{productId} AND o.id != #{excludeOrderId} " +
            "AND o.deleted = 0 AND o.paid = 1")
    int countProductPurchase(@Param("userId") String userId,
                           @Param("productId") String productId,
                           @Param("excludeOrderId") String excludeOrderId);

    /**
     * 获取用户对某商品的最新反馈
     * @param userId 用户ID
     * @param productId 商品ID
     * @return 最新反馈内容
     */
    @Select("SELECT f.message FROM order_feedbacks f " +
            "JOIN yshop_store_order_cart_info oi ON f.order_id = oi.oid " +
            "JOIN yshop_store_order o ON f.order_id = o.id " +
            "WHERE f.user_id = #{userId} AND oi.product_id = #{productId} " +
            "AND o.deleted = 0 " +
            "ORDER BY f.create_time DESC LIMIT 1")
    String getLatestFeedback(@Param("userId") String userId,
                           @Param("productId") String productId);
}